# -*- coding:utf-8 -*-
"""
键盘监控类
"""
import pythoncom
import pyHook
import GlobalVar


class Keyboard:
    def __init__(self, message_obj):
        self.asciiStr = ''
        self.keyStr = ''
        self.message = message_obj

    def monitor(self):
        hm = pyHook.HookManager()
        hm.KeyDown = self.on_keyboard_event_down
        hm.KeyUp = self.on_keyboard_event_up
        hm.HookKeyboard()
        pythoncom.PumpMessages(10000)

    def on_keyboard_event_down(self, e):
        """
        发送按键按下事件
        """
        if GlobalVar.get_lock() == 'unlock':
            self.message.send_keyboard('d' + str(e.KeyID))
            return False
        else:
            GlobalVar.set_lock('unlock')
            return True

    def on_keyboard_event_up(self, e):
        """
        发送按键弹起事件
        """
        if GlobalVar.get_lock() == 'unlock':
            self.message.send_keyboard('u' + str(e.KeyID))
            return False
        else:
            GlobalVar.set_lock('unlock')
            return True
